package com.qf.web.controller;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * projectName:xiaomi1
 * author:
 * time:2024/4/17 14:30
 * description:
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());

            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(user.getId());
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "预览订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String aid = request.getParameter("aid");
        //非空验证
        if (StringUtils.isEmpty(aid)) {
            request.setAttribute("msg", "地址不能为空");
            return "/message.jsp";
        }

        try {
            OrderService orderService = new OrderServiceImpl();
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            if (cartList == null || cartList.size() == 0) {
                request.setAttribute("msg", "购物车不能为空");
                return "/message.jsp";
            }
            BigDecimal money = new BigDecimal(0);
            for (Cart cart : cartList) {
                money = money.add(cart.getMoney());
            }
            String oid = RandomUtils.createOrderId();
            Order order = new Order(oid, user.getId(), money, "1", new Date(), Integer.parseInt(aid));
            orderService.submitOrder(order, cartList);
            request.setAttribute("order", order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }

        try {
            OrderService orderService = new OrderServiceImpl();
            List<Order> orderList = orderService.find(user.getId());
            request.setAttribute("orderList", orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //获取订单详情
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String oid = request.getParameter("oid");
        if (StringUtils.isEmpty(oid)){
            request.setAttribute("msg", "订单编号不能为空");
            return "/message.jsp";
        }
        try {
            //调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //查看订单
            Order order = orderService.findByOid(oid);
            //订单详情
            List<OrderDetail> orderDetails = orderService.findDetail(oid);
            //存值
            request.setAttribute("order",order);
            request.setAttribute("orderDetails",orderDetails);
            return  "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败：" + e.getMessage());
            return "/message.jsp";
        }

    }
}